﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using RRD.DAL;
using RRD.Model;
using System.Configuration;
using System.Windows.Media;

namespace RRD.Controller
{
    public class Scanner
    {
        private string codeBuffer = string.Empty;
        private Logger logger = new Logger();
        private Database database = new Database();
        private State state = new State();
        
        public void WriteBuffer(Key readChar)
        {
            try
            {
                codeBuffer += readChar.ToString();
            }
            catch (Exception ex)
            {
                logger.WriteLog(ex.Message);
            }
        }

        public void WriteBuffer(string readChar)
        {
            try
            {
                codeBuffer += readChar.ToString();
            }
            catch (Exception ex)
            {
                logger.WriteLog(ex.Message);
            }
        }

        public string ReadBuffer()
        {
            string value = string.Empty;
            try
            {
                value = codeBuffer;
            }
            catch (Exception ex)
            {
                logger.WriteLog(ex.Message);
            }
            return value;
        }

        public void ClearBuffer()
        {
            try
            {
                codeBuffer=string.Empty;
            }
            catch(Exception ex)
            {
                logger.WriteLog(ex.Message);
            }
        }

        public Message PutTrolley(string trolleyNumber, string truckNumber )
        {
            var value = new Message();
            try
            {
                if (this.ConfirmTrolleyNumber(trolleyNumber, truckNumber))
                {
                    //database.InsertTrolley(trolleyNumber, truckNumber);
                    value.MessageValue = "Trolley poprawny. Proszę załadować.";
                    value.MessageType = MessageType.Message;
                }
                else
                {
                    value.MessageValue = "Błąd - Trolley spoza ciężarówki.";
                    value.MessageType = MessageType.Error;
                }
            }
            catch (Exception ex)
            {
                logger.WriteLog(ex.Message);
            }
            return value;
        }

        private bool ConfirmTrolleyNumber(string trolleyNumber, string truckNumber)
        {
            bool value = false;
            try
            {
                value = true;
                /*
                if (database.ConfirmTrolleyNumber(trolleyNumber, truckNumber))
                    value = true;
                */ 
            }
            catch (Exception ex)
            {
                logger.WriteLog(ex.Message);
            }
            return value;
        }

        public Message CheckBarcode(string barcode)
        {
            var message = new Message() { MessageType = MessageType.Error, MessageValue = "" };

            if (barcode == ConfigurationManager.AppSettings.Get("OpenDock"))
            {
                message = state.OpenTruck();
            }
            else if (barcode == ConfigurationManager.AppSettings.Get("CloseDock"))
            {
                message.MessageValue = state.CloseTruck();
            }
            else if (barcode.Length.Equals(ConfigurationManager.AppSettings.Get("TruckNumber").Length) && barcode.Contains(ConfigurationManager.AppSettings.Get("TruckNumber").TrimEnd('x')))
            {
                if (state.GetStatus().CompareTo(StateEnum.ReadyToOpen) == 0)
                {
                    message = state.SetTruckNumber(barcode);
                }
                else
                {
                    message.MessageValue = "Co mam zrobić z tą ciężarówką ?";
                    message.MessageType = MessageType.Error;
                }
            }
            else if (barcode.Length.Equals(ConfigurationManager.AppSettings.Get("TrolleyNumber").Length) && barcode.Contains(ConfigurationManager.AppSettings.Get("TrolleyNumber").TrimEnd('x')))
            {
                if (state.GetStatus().CompareTo(StateEnum.Open) == 0)
                {
                    message = PutTrolley(barcode, state.GetTruckNumber());
                }
                else
                {
                    message.MessageValue = "Ciężarówka jest zamknięta. Co mam zrobić z tym trolley'em ?";
                    message.MessageType = MessageType.Error;
                }
            }
            else if (barcode.Length.Equals(ConfigurationManager.AppSettings.Get("EmployeeNumber")) && barcode.Contains(ConfigurationManager.AppSettings.Get("EmployeeNumber").TrimEnd('x')))
            {

            }
            return message;
        }

        public Brush GetTruckStatus()
        {
            var brush = Brushes.White;
            switch (state.GetStatus())
            {
                case StateEnum.Open:
                    brush = Brushes.Green;
                    break;
                case StateEnum.ReadyToOpen:
                    brush = Brushes.Orange;
                    break;
                case StateEnum.Close:
                    brush = Brushes.Red;
                    break;
            }
            return brush;
        }

        public Brush GetDBStatus(DatabaseType type)
        {
            var brush = Brushes.Red;
            if (database.IsDBConnected(type))
            {
                brush = Brushes.Green;
            }
            else
            {
                brush=Brushes.Red;
            }
            return brush;
        }
    }
}
